/*
 * @Author: Hp_lucky 1063512385@qq.com
 * @Date: 2024-09-18 09:11:28
 * @LastEditors: Hp_lucky 1063512385@qq.com
 * @LastEditTime: 2024-09-20 17:13:59
 * @FilePath: \cocos_frame\assets\script\src\Basic\Model\Tips\comp\DropdownList.ts
 */
import { _decorator } from 'cc';
import { PoolFactory } from '../../../Component/PoolFactory';
import { List } from '../../../Component/List';
import { DropDownListData } from '../TipsDataBase';
import { BaseEventType } from '../../../Config/BaseEnum';
import { EventManager } from '../../../Manager/EventManager';
import { NodeRecycle } from '../../../Manager/PoolManager';
import { DropdownBox } from '../../../Component/DropdownBox';

const { ccclass, property, menu } = _decorator;

@ccclass('DropdownList')
@menu('myComponent/DropdownList')
export class DropdownList extends NodeRecycle {
  @property(PoolFactory)
  itemFactory: PoolFactory;
  @property(List)
  list: List;

  target: DropdownBox;
  listData: DropDownListData;
  reuse(data: DropDownListData) {
    this.listData = data;
    this.target = this.listData.node.getComponent(DropdownBox);
    EventManager.Ins().register(BaseEventType.GAME_DROPDOWN_CHANGE, this.onChange, this);
  }

  unuse(): void {
    EventManager.Ins().unregister(BaseEventType.GAME_DROPDOWN_CHANGE, this.onChange);
  }

  protected onEnable(): void {
    this.list.setData(this.listData.data);
  }

  onChange(text: string) {
    if (!this.listData.judgeFun) return;
    const arr = [];
    this.listData.data.forEach((element) => {
      if (this.listData.judgeFun.call(this.target, text, element)) {
        arr.push(element);
      }
    });
    this.list.setData(arr);
  }

  onClick(...args) {
    this.listData.onChose.call(this.target, ...args);
  }
}
